/*
 * @Description:
 * @Version: 2.0
 * @Autor: 黄康辉
 * @Date: 2021-11-15 17:12:07
 * @LastEditors: kafei
 * @LastEditTime: 2021-12-21 16:48:04
 */
const Koa = require('koa')
const app = new Koa()
const bodyparser = require('koa-bodyparser')
const router = require('./routes/index.js')
const koajwt = require('koa-jwt')
const { key } = require('./config/key')
const result = require('./utils/result')
// const cors = require('koa2-cors')
// const { historyApiFallback } = require('koa2-connect-history-api-fallback')

// app.use(historyApiFallback({
//   index: '/index.html'
// })).use(require('koa-static')('./dist'))
// app.use(cors())

app.use(function(ctx, next) {
  return next().catch((err) => {
    if (err.status === 401) {
      ctx.status = 401
      ctx.body = result('0', 'Protected resource, use Authorization header to get access')
    } else {
      throw err
    }
  })
})

app.use(koajwt({ secret: key }).unless({
  path: [
    /^\/api\/login/, // 登陆接口
    /^\/api\/register/, // 注册
    /^\/api\/userAA/
  ]
}))

app.use(bodyparser())
app.use(router.routes()).use(router.allowedMethods())

app.listen(4000)
